<?php
require_once 'paypal.class.php';
/**
 * LOG:
 * <pre>
 * 'components'=>array( * 	...... * 	'log'=>array( * 		'class'=>'CLogRouter', * 		'routes'=>array( * 			array( * 				'class'=>'CFileLogRoute', * 				'levels'=>'', * 				'categories'=>'ext.epaypal.*', * 				'logFile' => 'paypal.log' * 			), * 		... * 		), * 	), * ),
 * </pre>
 */
class EPayPal extends CApplicationComponent {
	const PAYPAL_HOST='www.paypal.com';
	const PAYPAL_SANDBOX_HOST='www.sandbox.paypal.com';
	const LOG_CATEGORY_IPN='ext.paypal.ipn';
	public $debug=false;

	public $business;


	private $_returnUrl='';
	private $_cancelUrl='';
	private $_notifyUrl='';
	private $_itemName='';
	private $_currencyCode='AUD';
	private $_amount='0.0';
	private $_custom='';

	private $p;

	public function init(){
		parent::init();
		$this->p=new paypal_class($this->debug);
		$p=$this->p;
		$p->add_field('business', $this->business);
		$p->add_field('return', $this->_returnUrl);
		$p->add_field('cancel_return', $this->_cancelUrl);
		$p->add_field('notify_url', $this->_notifyUrl);
		$p->add_field('item_name',  $this->_itemName);
		$p->add_field('currency_code', $this->_currencyCode);
		$p->add_field('amount',$this->_amount);
		$p->add_field('custom', $this->_custom);
	}


	public function log($message,$level=CLogger::LEVEL_INFO,$category='ext.epaypal'){
		Yii::log($message, $level,$category);
	}

	public function getUrl(){
		return $this->p->paypal_url;
	}

	public function validateIpn(){
		$this->log('IPN Payment coming... POST Data: '.print_r($_POST,true));
		$validateSuccess=$this->p->validate_ipn(true);
		if($validateSuccess){
				
		}else{
			$body =  "An instant payment notification was failed, the fail reson is ".$this->ipnStatus."\n";
			$this->log($body);
		}
		return $validateSuccess;
	}

	public function setReturnUrl($returnUrl){
		$this->_returnUrl=$returnUrl;
		if($this->p){
			$this->p->add_field('return', $this->_returnUrl);
		}
	}
	public function setCancelUrl($cancelUrl){
		$this->_cancelUrl=$cancelUrl;
		if($this->p){
			$this->p->add_field('cancel_return', $this->_cancelUrl);
		}
	}

	public function setNotifyUrl($notifyUrl){
		$this->_notifyUrl=$notifyUrl;
		if($this->p){
			$this->p->add_field('notify_url', $this->_notifyUrl);
		}
	}
	public function setItemName($itemname){
		$this->_itemName=$itemname;
		if($this->p){
			$this->p->add_field('item_name', $this->_itemName);
		}
	}

	public function setCurrencyCode($currency){
		$this->_currencyCode=$currency;
		if($this->p){
			$this->p->add_field('currency_code', $this->_currencyCode);
		}
	}

	public function setAmount($amount){
		$this->_amount=$amount;
		if($this->p){
			$this->p->add_field('amount', $this->_amount);
		}
	}
	public function setCustom($custom){
		$this->_custom=$custom;
		if($this->p){
			$this->p->add_field('custom', $this->_custom);
		}
	}
	public function getFields(){
		return  $this->p->fields;
	}

	public function getIpnStatus(){
		return  $this->p->ipn_status;
	}
	public function getIpnData(){
		return  $this->p->ipn_data;
	}



}